import request from "../utils/request";
import Qs from 'qs';

export function login(username, password) {
    return new Promise((resolve, reject) => {
        request.post("/user/login", Qs.stringify({
            username,
            password
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function resetPassword(id, oldPassword, newPassword) {
    return new Promise((resolve, reject) => {
        request.post("/user/resetPassword", Qs.stringify({
            id, oldPassword, newPassword
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}


export function adminAddBlog(title, cover, content) {
    return new Promise((resolve, reject) => {
        request.post("/blog/add", Qs.stringify({
            title,
            cover,
            content
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function adminDelBlog(id) {
    return new Promise((resolve, reject) => {
        request.post("/blog/deleteById", Qs.stringify({
            id,
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

// 获取用户列表
export function userList(currentPage, pageSize) {
    return new Promise((resolve, reject) => {
        request.post("/user/list", Qs.stringify({
            currentPage,
            pageSize,
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

// 修改自己的名称
export function renameNickName(nickName) {
    return new Promise((resolve, reject) => {
        request.post("/user/renameNickName", Qs.stringify({
            nickName
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function deleteDownloadFile(id) {
    return new Promise((resolve, reject) => {
        request.post("/file/delete", Qs.stringify({
            id
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function addDownloadFile(name, filePath, cover, type) {
    return new Promise((resolve, reject) => {
        request.post("/file/add", Qs.stringify({
            name, filePath, cover, type
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}


export function getUserById(id) {
    return new Promise((resolve, reject) => {
        request.post("/user/get/" + id).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function updateHead(imgPath) {
    return new Promise((resolve, reject) => {
        request.post("/user/updateHead", Qs.stringify({
            imgPath
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function sendYzm(email) {
    return new Promise((resolve, reject) => {
        request.post("/user/sendYzm", Qs.stringify({
            email
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function findPassword(email,yzm) {
    return new Promise((resolve, reject) => {
        request.post("/user/findPassword", Qs.stringify({
            email,
            yzm
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}
